#include "types.h"
#include "stat.h"
#include "user.h"

#define PROCS 3
void
childPrint(int cid)
{

	int j;
	for (j=0; j<500; j++)
	{
		printf(1,"child's cid is %d. print number: %d\n",cid,j+1);
	}
}

void 
sanity(void)
{

	int i,k,t,pid,cpid,wTime,rTime,tTime,pcid,cid;

	//total
	float totalWaiting = 0;
	float totalRunning = 0;
	float totalTurnaround = 0;

	//high
	float highWaiting = 0;
	float highRunning = 0;
	float highTurnaround = 0;

	//medium
	float mediumWaiting = 0;
	float mediumRunning = 0;
	float mediumTurnaround = 0;

	//low
	float lowWaiting = 0;
	float lowRunning = 0;
	float lowTurnaround = 0;

	int ans[PROCS][3];
	for (i=0; i<PROCS; i++)
	{
		pid=fork();
		cid = i;
		if (cid % 3 == 0)
		{
			nice();
		}
		else if (cid % 3 == 1)
		{
			nice();
			nice();
			printf(1,"da");
		}
		if (pid == 0)
		{
			childPrint(cid);
			exit();
		}
	}
	for (k=0; k<PROCS; k++)
	{
		int turnaround;
		cpid=wait2(&wTime,&rTime);	
		ans[k][0] = cpid;
		ans[k][1] = wTime;
		ans[k][2] = rTime;	
		turnaround = wTime+rTime;

		totalRunning+=rTime;
		totalWaiting+=wTime;
		totalTurnaround+=turnaround;

		if (cid % 3 == 2)
		{
			highRunning+=rTime;
			highWaiting+=wTime;
			highTurnaround+=turnaround;
		}
		else if (cid % 3 == 1)
		{
			lowRunning+=rTime;
			lowWaiting+=wTime;
			lowTurnaround+=turnaround;
		}
		else
		{
			mediumRunning+=rTime;
			mediumWaiting+=wTime;
			mediumTurnaround+=turnaround;
		}

	}

	printf(1,"\n\ntotal results\n total average running time: %d\ntotal average waiting time: %d\ntotal average turnaround time: %d", totalWaiting/PROCS,totalWaiting/PROCS,totalTurnaround/PROCS);
	printf(1,"\n\ngroup results:\n\nHIGH:\naverage running time: %d\naverage waiting time: %d\naverage turnaround time: %d\n\n\n",highRunning/10,highWaiting/10,highTurnaround/10);
	printf(1,"\n\ngroup results:\n\nMEDIUM:\naverage running time: %d\naverage waiting time: %d\naverage turnaround time: %d\n\n\n",mediumRunning/10,mediumWaiting/10,mediumTurnaround/10);
	printf(1,"\n\ngroup results:\n\nLOW:\naverage running time: %d\naverage waiting time: %d\naverage turnaround time: %d\n",lowRunning/10,lowWaiting/10,lowTurnaround/10);
	printf(1,"\n\nfinal single results:\n");
	for (t=0; t<PROCS; t++)
	{
		tTime = ans[t][1]+ans[t][2];
		printf(1,"child %d stats: waiting-time = %d running-time = %d turnaround-time = %d\n",ans[t][0],ans[t]	[1],ans[t][2],tTime);
	}
}

int
main(void)
{
	sanity();
	exit();
}
